package bm.com.project.server;

import bm.com.framework.redis.keys.RedisKey;
import bm.com.framework.redis.unit.RedisUnits;
import bm.com.framework.common.unit.Tool;
import bm.com.framework.web.core.dto.BmSysUserTokenDTO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class VerifyController {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    RedisUnits redisUnits;

    /**
     * 通过Shiro 获取token中的信息
     *
     * @return
     */
    protected BmSysUserTokenDTO getUserToken() {

        return new BmSysUserTokenDTO();
//        return (BmSysUserTokenDTO) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 获取登陆者 UserID
     *
     * @return
     */
    protected String getUserId() {
        return getUserToken().getUserId();
    }

    /**
     * 获取 组织ID
     *
     * @return
     */
    protected String getOrgId() {
        return getUserToken().getOrgId();
    }

    /**
     * 通过token 解析token中的信息
     *
     * @param token
     * @return
     */
    protected BmSysUserTokenDTO getUserByToken(String token) {
        BmSysUserTokenDTO userToken = null;
        if ("".equals(token) || token == null) {
            return userToken;
        }

        String value = redisUnits.get(RedisKey.sysUserToken(token));
        if (Tool.isStringEmpty(value)) {
            userToken = Tool.fromJson(value, BmSysUserTokenDTO.class);
        }
        return userToken;
    }

}
